import 'package:dart_sdk/protos/proto_message.pb.dart';
import 'package:dart_sdk/websocket-io.dart';
import 'package:dart_sdk_example/channel_list.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyWidget());
}

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(primarySwatch: Colors.blue, useMaterial3: false),
        home: MyApp());
  }
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late WebSocketIO webSocketIO = WebSocketIO(
    onConnectionStatusChanged: (String value) {
      setState(() {});
    },
    onMessageReceived: (MessageProtoBuf value) {},
    onKickoutMessageReceived: (value) {
      showDialog(
          context: context,
          builder: (builder) {
            return AlertDialog(
              title: const Text("被踢下线"),
              content: const Text("你的账号在其他地方登录，你被迫下线"),
              actions: [
                ElevatedButton(
                    onPressed: () {
                      webSocketIO.connect();
                      Navigator.pop(context);
                    },
                    child: const Text("重新登录"))
              ],
            );
          });
    },
  );

  @override
  void initState() {
    super.initState();
    webSocketIO.addListener(() {
      setState(() {});
    });
    webSocketIO.init(wsUrl, identity, channels, baseUrl);
    webSocketIO.connect();
  }

  final List<String> channels = [
    'Qbs5gUVpjh4jgP8ws5s708J7rOZHV8kSB8a8',
    'Qbs5gUVpjh4jgP8ws5s708J7rOZHV8kSB8a7'
  ];
  final String baseUrl = "http://47.109.95.36:7003";
  final String wsUrl = "ws://47.109.95.36:7001";
  final String identity = "1234567890111";

  @override
  void dispose() {
    webSocketIO.removeListener(() {});
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(webSocketIO.connectionStatus),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {},
        child: const Icon(Icons.send),
      ),
      body: Row(
        children: [
          Container(
              width: 350,
              decoration: const BoxDecoration(
                  border:
                      Border(right: BorderSide(color: Colors.grey, width: 1))),
              child: ChannelList(
                channels: webSocketIO.getChannels.reversed.toList(),
                selectedChannel: webSocketIO.getSelectedChannel,
                onSelectedChannelChanged: (String value) {
                  webSocketIO.selectChannel(value);
                },
              )),
          Expanded(
              child: ListView(
            children: List.generate(
              webSocketIO.getMessages.length,
              (index) => Container(
                decoration: BoxDecoration(
                    border: Border(
                        bottom:
                            BorderSide(color: Colors.grey.withOpacity(0.5)))),
                child: ListTile(
                  dense: true,
                  title: Text(webSocketIO.messages[index].channel),
                  subtitle: Text(webSocketIO.messages[index].data),
                  trailing:
                      Text(webSocketIO.messages[index].timestamp.toString()),
                ),
              ),
            ),
          ))
        ],
      ),
    );
  }
}
